package com.bdqn;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;

import java.nio.charset.Charset;

/**
 *   @SpringBootApplication 来标注一个主程序类，说明这是一个Spring Boot应用
 *
 *   @ImportResource：导入Spring的配置文件，让配置文件里面的内容生效；
 *    Spring Boot里面没有Spring的配置文件，我们自己编写的配置文件，也不能自动识别；
 *    想让Spring的配置文件生效，加载进来；@ImportResource标注在一个配置类上
 *    @ImportResource(locations = {"classpath:beans.xml"})
 *    即： 加载spring配置文件的
 *    @MapperScan("com.bdqn.augiugn.dao")//使用MapperScan批量扫描所有的Mapper接口；
 */
@SpringBootApplication
@MapperScan("com.bdqn.dao")//使用MapperScan批量扫描所有的Mapper接口；
public class SpringBootFastApplication {
    //Spring启动起来
    public static void main(String[] args) {
        SpringApplication.run(SpringBootFastApplication.class, args);
    }

     //响应字符串时走这个
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

     //响应json对象时走这个
    @Bean
    public HttpMessageConverters fastjsonHttpMessageConvert(){
        FastJsonHttpMessageConverter fastJsonMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        //config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        config.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat);//返回的对象含有日期属性，格式 年月日 时分秒
        fastJsonMessageConverter.setFastJsonConfig(config);
        HttpMessageConverter<?> messageConvert = fastJsonMessageConverter;
        return new HttpMessageConverters(messageConvert);
    }
}
